package de.karlNet.sambamanager.view.beans.forms;

import javax.naming.NamingException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Controller;

import de.karlNet.sambamanager.ldapController.commands.RenameLdapObject;
import de.karlNet.sambamanager.ldapController.commands.macros.ModifyARecordMacro;
import de.karlNet.sambamanager.ldapModel.dns.LdapDNSARecord;
import de.karlNet.sambamanager.ldapModel.dto.ModifyARecordDTO;
import de.karlNet.sambamanager.view.beans.trees.SelectedNodeContainer;

@Controller
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class EditARecordBean extends ViewBean {
	@Autowired
	private RenameLdapObject renameLdapObject;

	@Autowired
	protected SelectedNodeContainer nodeContainer;

	@Autowired
	private ModifyARecordMacro modifyARecordMacro;

	private LdapDNSARecord ldapDNSARecord;
	private LdapDNSARecord aRecord;

	public LdapDNSARecord getaRecord() {
		return aRecord;
	}

	public void setShow(boolean show) {

		this.aRecord = (LdapDNSARecord) this.nodeContainer.selectedNode
				.getLdapObject();
		this.ldapDNSARecord = new LdapDNSARecord(this.aRecord.getCn(),
				this.aRecord.getDn(), this.aRecord.getIpAddress(),
				this.aRecord.getTtl());
		super.setShow(show);
	}

	@Override
	public String getPageName() {
		return "editRecordView";
	}

	public String create() throws NamingException {
		try {
			ModifyARecordDTO modifyARecordDTO = new ModifyARecordDTO(
					ldapDNSARecord, aRecord);
			modifyARecordMacro.modifyARecord(modifyARecordDTO);
			this.setShow(false);
			return "index.xhtml";
		} catch (Exception e) {
			this.showErrorMessage(e);
			return "";
		}
	}
}
